#include "../include/file_helper.h"
#include "../include/standard_library.h"


// 返回0完全传输成功,返回-1出现错误,大于0的表示此次传输的长度
int sendFile_7th(int to_netfd, char file_path[], off_t off_set) {
	int fd = open(file_path, O_RDONLY);
	if(fd <= 0)
		return -1;
	struct stat st;
	fstat(fd,&st);
	return sendfile(to_netfd,fd,&off_set,st.st_size-off_set);
}

// 返回0完全传输成功,返回-1出现错误,大于0的表示此次传输的长度
int recvFile_7ht(int in_netfd, char file_path[], off_t file_size, off_t offset) {
	int fd = open(file_path, O_RDWR | O_CREAT);
	if(fd <= 0)
		return -1;
	ftruncate(fd,file_size);
	char *file_head= mmap(NULL,file_size,PROT_WRITE,MAP_SHARED,fd,0);
	char *dd_splic=file_head+offset;
	int len=recv(in_netfd,dd_splic,file_size-offset,MSG_WAITALL);
	if(len<=0)
	{
		printf("接收文件过程出错\n");
		close(fd);
		return -1;
	};

	msync(file_head, file_size, MS_SYNC);
	munmap(file_head,file_size);
	close(fd);
	
	return len;
}